package com.chengh.springboot.demo.demo01;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 常用Demo案例
 * 在循环一组User对象时，根据字段判断User是否重复，如果重复，不做任何操作。
 */
public class Test {

    public static void main(String[] args) {
        List<UserInfo> userInfos = new ArrayList<>();
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setUserId(1L);
        userInfo1.setUserName("张三");
        userInfo1.setCarName("保时捷");
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setUserId(1L);
        userInfo2.setUserName("张三");
        userInfo2.setCarName("路虎");
        userInfos.add(userInfo1);
        userInfos.add(userInfo2);

        UserInfo userInfo3 = new UserInfo();
        userInfo3.setUserId(2L);
        userInfo3.setUserName("李四");
        userInfo3.setCarName("五菱宏光");
        UserInfo userInfo4 = new UserInfo();
        userInfo4.setUserId(2L);
        userInfo4.setUserName("李四");
        userInfo4.setCarName("宝马");
        userInfos.add(userInfo3);
        userInfos.add(userInfo4);

        List<User> users = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            Optional<User> first = users.stream().filter(item -> item.getUserId() == userInfo.getUserId()).findFirst();
            if(!first.isPresent()) {
                User user = new User();
                user.setUserId(userInfo.getUserId());
                user.setUserName(userInfo.getUserName());
                users.add(user);
            }
        }

        System.out.println(users);
    }
}
